package com.hlkj.warboot.configurer.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.UUID;

/**
 * @Description: 文件和base64字符串的互相转化
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/4/1 12:57
 */
public class Base64Util {

    public static void main(String[] args) throws IOException {
//        String base64 = imgToBase64("C:\\Users\\lixing\\Desktop\\辽宁轻销售渠道分类.png");
//        System.out.println(base64);
//        System.out.println(base64ToImg(base64,"C:\\Users\\lixing\\Desktop", "jpg"));

//        String base64 = fileToBase64("C:\\Users\\lixing\\Desktop\\视频.mp4");
//        String base64 = fileToBase64("C:\\Users\\lixing\\Desktop\\图片.jpg");
//        System.out.println(base64);
//        System.out.println(base64ToFile(base64,"jpg","C:\\Users\\lixing\\Desktop"));
    }

    /*
     * @Description 将图片转换成base64
     * @Author 李星
     * @Date 2020/4/1 13:05
     * @Param [path 本地图片绝对路径，格式: c:\\123.png]
     * @return java.lang.String
     */
    public static String imgToBase64(String path) throws IOException {
        int originalFileTypeLastIndex = path != null ? path.lastIndexOf(".") : 0;
        if (originalFileTypeLastIndex == 0) {
            return null;
        }
        String originalFileType = path.substring(originalFileTypeLastIndex + 1).toLowerCase(); // 扩展名 格式：png

        File file = new File(path);
        BufferedImage image = ImageIO.read(file);
        String base64 = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, originalFileType, byteArrayOutputStream);
            byte[] imageBytes = byteArrayOutputStream.toByteArray();
            base64 = Base64.getEncoder().encodeToString(imageBytes);
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            byteArrayOutputStream.close();
        }
        return base64;
    }

    /*
     * @Description 将文件转化成base64
     * @Author 李星
     * @Date 2020/4/1 14:00
     * @Param [filePath 本地文件绝对路径，格式: c:\\123.mp4]
     * @return java.lang.String
     */
    public static String fileToBase64(String filePath) throws IOException {
        if (filePath == null || filePath.length() == 0) {
            return null;
        }
        String base64 = null;
        FileInputStream fileInputStream = new FileInputStream(new File(filePath));
        try {
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[fileInputStream.available()];
            // 将文件中的内容读入到数组中
            fileInputStream.read(bytes);
            base64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
            fileInputStream.close();
            base64 = base64.replaceAll("\n", "");
        } catch (IOException fe) {
            fe.printStackTrace();
            fileInputStream.close();
        }
        return base64;
    }

    /*
     * @Description 将base64转换成指定格式的文件存储到自定路径
     * @Author 李星
     * @Date 2020/4/1 13:34
     * @Param [base64, type 文件格式，格式：jpg|mp4, path 相对路径，格式：C:\\file ]
     * @return java.lang.String
     */
    public static String base64ToFile(String base64, String fileType, String filePath) {
        if (base64 == null || base64.length() == 0) {
            return null;
        }
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imageByte = decoder.decodeBuffer(base64); // 即将base64字符串转换成二进制流

            String absoultPath = filePath + File.separator + UUID.randomUUID().toString() + "." + fileType.toLowerCase();

            OutputStream out = new FileOutputStream(absoultPath);
            out.write(imageByte);
            out.flush();
            out.close();
            return absoultPath;
        } catch (IOException e) {
            return null;
        }
    }

}
